package com.ice.work.moment.user.service.resource;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.honeybee.cloud.framework.common.constant.CommonSystemConstant;
import com.honeybee.cloud.framework.common.enums.DataStatusEnum;
import com.honeybee.cloud.framework.common.idwork.IdWorker;
import com.honeybee.cloud.framework.common.page.Page;
import com.ice.work.moment.facade.user.dto.ResourceDTO;
import com.ice.work.moment.facade.user.enums.RoleTypeEnum;
import com.ice.work.moment.facade.user.mapper.ResourceConvertMapper;
import com.ice.work.moment.facade.user.model.Resource;
import com.ice.work.moment.facade.user.model.Role;
import com.ice.work.moment.facade.user.model.RoleToResource;
import com.ice.work.moment.facade.user.request.PageResourceRequest;
import com.ice.work.moment.user.dao.resource.ResourceRepository;
import com.ice.work.moment.user.dao.role.RoleRepository;
import com.ice.work.moment.user.dao.roletoresource.RoleToResourceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @Description ResourceServiceImpl
 * @Author DELL
 * @Create 2019-01-10
 * @Since 1.0.0
 */
@Service
public class ResourceServiceImpl implements IResourceService {


    @Autowired
    private ResourceRepository resourceRepository;

    @Autowired
    private RoleRepository repository;
    @Autowired
    private RoleToResourceRepository roleToResourceRepository;

    @Autowired
    private IdWorker idWorker;

    @Override
    public List<ResourceDTO> findByRoleId(Long roleId) {
        return null;
    }

    @Override
    public List<ResourceDTO> findByRoleIds(Collection<Long> roleId) {
        return null;
    }

    @Override
    public Boolean save(Collection<Resource> resources) {
        return resourceRepository.saveBatch(resources, CommonSystemConstant.BATCH_SIZE);
    }

    @Override
    public Boolean saveOrUpdateBySign(Collection<Resource> resources) {
        if (CollectionUtils.isEmpty(resources)) {
            return true;
        }
        List<Resource> notExistsList = Lists.newArrayList();
        List<Resource> existsList = Lists.newArrayList();
        for (Resource resource : resources) {
            Resource exists = resourceRepository.findBySignAndDataStatus(resource.getSign(), DataStatusEnum.VALID.key);
            if (Objects.nonNull(exists)) {
                resource.setId(exists.getId());
                existsList.add(resource);
            } else {
                resource.setId(idWorker.nextId());
                notExistsList.add(resource);
            }
        }
        List<Role> roleDTOS = null;
        if (!CollectionUtils.isEmpty(notExistsList)) {
            roleDTOS = repository.findByType(RoleTypeEnum.SUPER.key);
            resourceRepository.saveBatch(notExistsList, CommonSystemConstant.BATCH_SIZE);

        }
        if (!CollectionUtils.isEmpty(existsList)) {
            resourceRepository.updateBatchById(existsList);
        }

        //将新增的权限给予超级管理员
        if (!CollectionUtils.isEmpty(notExistsList) && !CollectionUtils.isEmpty(roleDTOS)) {
            List<RoleToResource> roleToResources = Lists.newArrayList();
            for (Role roleDTO : roleDTOS) {
                for (Resource resource : notExistsList) {
                    RoleToResource roleToResource = new RoleToResource();
                    roleToResource.setId(idWorker.nextId());
                    roleToResource.setResourceId(resource.getId());
                    roleToResource.setRoleId(roleDTO.getId());
                    roleToResources.add(roleToResource);
                }
            }
            roleToResourceRepository.saveBatch(roleToResources, CommonSystemConstant.BATCH_SIZE);
        }

        return true;
    }

    @Override
    public Page<ResourceDTO> page(IPage<Resource> page, PageResourceRequest resourceRequest) {
        IPage<Resource> resourceIPage = resourceRepository.page(page, resourceRequest);
        return ResourceConvertMapper.INSTANCE.toDTO(resourceIPage);
    }


}
